package com.dgut.epidemic.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExcelUtil {
    public static void writeExcel(HttpServletResponse response, List lists,List<String> head) throws IOException {
        if (lists.size() <= 0) return;
        List<List<String>> heads = new ArrayList<>();
        for(String str : head){
            heads.add(Arrays.asList(str));
        }
        String fileName = lists.get(0).getClass().getSimpleName();
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.addHeader("Content-Disposition", "filename=" + fileName + ".xlsx");
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        WriteSheet writeSheet = new WriteSheet();

        WriteSheet sheet = EasyExcel.writerSheet(0, "sheet").head(heads).build();
        excelWriter.write(lists, sheet);
        excelWriter.finish();
    }

}
